package zero.tech.games.log.hot;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import zero.tech.core.data.PageBean;
import zero.tech.games.api.RemoteUser;
import zero.tech.games.common.RedisConsts;
import zero.tech.games.entity.log.LogTrade;
import zero.tech.games.entity.user.TUser;
import zero.tech.games.log.data.LogTradeInfo;
import zero.tech.games.log.mapper.LogTradeMapper;
import zero.tech.redis.cache.RedisUtil;

@Component
public class LogTradeUtil {
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private RemoteUser remoteUser;
	@Autowired
	private LogTradeMapper logTradeMapper;
	@SuppressWarnings("unchecked")
	public PageBean<LogTradeInfo> listLogTradeByPage(int uid, int club_id, int page, int pageLength, Date begin, Date end) {
		PageBean<LogTradeInfo> pageBean=(PageBean<LogTradeInfo>) redisUtil.hget(RedisConsts.HOT_LOG_TRADE_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime());
		if(pageBean==null) {
			int totalCount=logTradeMapper.totalCount(club_id,uid,begin,end);
			int start=page*pageLength;
			List<LogTradeInfo> result=new ArrayList<LogTradeInfo>();
			List<LogTrade> logTrades=logTradeMapper.listTradeByPage(club_id,uid,start,pageLength,begin,end);
			if(!logTrades.isEmpty()) {
				List<Integer> uids=new ArrayList<>();
				for(LogTrade logTrade:logTrades) {
					if(!uids.contains(logTrade.getUid())) {
						uids.add(logTrade.getUid());
					}
					if(!uids.contains(logTrade.getOther_uid())) {
						uids.add(logTrade.getOther_uid());
					}
				}
				Map<Integer,TUser> usermap=new HashMap<>();
				List<TUser> users=remoteUser.getUserBatch(uids);
				for(TUser user:users) {
					usermap.put(user.getUid(), user);
				}
				for(LogTrade logTrade:logTrades) {
					result.add(packageLogTradeInfo(logTrade,usermap));
				}
			}
			pageBean = new PageBean<>(totalCount, pageLength, page, result);
			redisUtil.hset(RedisConsts.HOT_LOG_WATER_TEAM_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime(), pageBean,5*60);
		}
		return pageBean;
	}
	private LogTradeInfo packageLogTradeInfo(LogTrade logTrade, Map<Integer, TUser> usermap) {
		LogTradeInfo logTradeInfo = new LogTradeInfo();
		int uid=logTrade.getUid();
		int other_uid=logTrade.getOther_uid();
		TUser user=usermap.get(uid);
		TUser other=usermap.get(other_uid);
		logTradeInfo.setClub_id(logTrade.getClub_id());
		logTradeInfo.setCreate_stamp(logTrade.getCreate_stamp());
		logTradeInfo.setDay_str(logTrade.getDay_str());
		logTradeInfo.setGold(logTrade.getGold());
		logTradeInfo.setHead_img(user.getHead_img());
		logTradeInfo.setNick(user.getNick());
		logTradeInfo.setOperate(logTrade.getOperate());
		logTradeInfo.setOther_head_img(other.getHead_img());
		logTradeInfo.setOther_nick(other.getNick());
		logTradeInfo.setOther_uid(other_uid);
		logTradeInfo.setUid(uid);
		logTradeInfo.setCur_tea(logTrade.getCur_tea());
		logTradeInfo.setCur_safety(logTrade.getCur_safety());
		logTradeInfo.setCur_frozen(logTrade.getCur_frozen());
		return logTradeInfo;
	}
	@SuppressWarnings("unchecked")
	public PageBean<LogTradeInfo> listLogTradeByPageAndUid(int uid, int club_id, int page, int pageLength, Date begin,
			Date end, Integer other_uid) {
		PageBean<LogTradeInfo> pageBean=(PageBean<LogTradeInfo>) redisUtil.hget(RedisConsts.HOT_LOG_TRADE_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime()+"_"+other_uid);
		if(pageBean==null) {
			int totalCount=logTradeMapper.totalCountByUid(club_id,uid,begin,end,other_uid);
			int start=page*pageLength;
			List<LogTradeInfo> result=new ArrayList<LogTradeInfo>();
			List<LogTrade> logTrades=logTradeMapper.listTradeByPageAndUid(club_id,uid,start,pageLength,begin,end,other_uid);
			if(!logTrades.isEmpty()) {
				List<Integer> uids=new ArrayList<>();
				for(LogTrade logTrade:logTrades) {
					if(!uids.contains(logTrade.getUid())) {
						uids.add(logTrade.getUid());
					}
					if(!uids.contains(logTrade.getOther_uid())) {
						uids.add(logTrade.getOther_uid());
					}
				}
				Map<Integer,TUser> usermap=new HashMap<>();
				List<TUser> users=remoteUser.getUserBatch(uids);
				for(TUser user:users) {
					usermap.put(user.getUid(), user);
				}
				for(LogTrade logTrade:logTrades) {
					result.add(packageLogTradeInfo(logTrade,usermap));
				}
			}
			pageBean = new PageBean<>(totalCount, pageLength, page, result);
			redisUtil.hset(RedisConsts.HOT_LOG_WATER_TEAM_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime()+"_"+other_uid, pageBean,5*60);
		}
		return pageBean;
	}
	public void insertBatch(List<LogTrade> logTrades) {
		logTradeMapper.insertBatch(logTrades);
		List<String> list=new ArrayList<String>();
		for(LogTrade logTrade:logTrades) {
			int club_id=logTrade.getClub_id();
			int uid=logTrade.getUid();
			String str=club_id+"_"+uid;
			if(!list.contains(str)) {
				list.add(str);
			}
		}
		for(String str:list) {
			redisUtil.del(RedisConsts.HOT_LOG_WATER_TEAM_PAGE+"_"+str);
		}
	}
}
